package com.tbi.graphcolouring.datacollector;

import java.util.ArrayList;
import java.util.List;

import com.tbi.algorithm.datacollector.IStopperDataCollector;
import com.tbi.graphcolouring.PopulationEntity;

public class AlgorithmStopDataCollector implements IStopperDataCollector{
	private int generations;
	private List<PopulationEntity> bestEntities = new ArrayList<>();
	private List<PopulationEntity> worstEntities = new ArrayList<>();
	private List<Float> averageEntity = new ArrayList<>();
	private PopulationEntity bestEver = new PopulationEntity(0);

	@Override
	public void collectData(List<Object> population, int generation) {
		float counter = 0;
		float sum = 0;
		PopulationEntity best = new PopulationEntity(0);
		PopulationEntity worst = new PopulationEntity(0);
		worst.rating = 0;
		this.generations = generation;
		
		for(Object obj : population){
			PopulationEntity entity = (PopulationEntity) obj;
			sum += entity.rating;
			counter++;
			if(entity.rating < best.rating){
				best = entity;
				if(best.rating < bestEver.rating){
					bestEver = best;
				}
			}
			if(entity.rating > worst.rating){
				worst = entity;
			}
		}
		
		worstEntities.add(worst);
		bestEntities.add(best);
		averageEntity.add(sum/counter);
	}
	
	public int getGenerations() {
		return generations;
	}
	
	public PopulationEntity getBestEntity(int generation) {
		return bestEntities.get(generation);
	}
	
	public float getAverageEntity(int generation) {
		return averageEntity.get(generation);
	}
	
	public PopulationEntity getBestEver() {
		return bestEver;
	}
	
	public PopulationEntity getWorstEntity(int generation) {
		return worstEntities.get(generation);
	}
}